package com.ymatou.iapi.godmch.testcase;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.json.JSONArray;
import org.json.JSONException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.sun.org.apache.bcel.internal.generic.NEW;
import com.ymatou.iapi.dguser.parameter.UserCreateBean;
import com.ymatou.iapi.godmch.parameter.addProviderBean;
import com.ymatou.iapi.godmch.parameter.createmchBean;
import com.ymatou.iapi.godmch.parameter.getProviderCountryBean;
import com.ymatou.iapi.godmch.service.addProviderCall;
import com.ymatou.iapi.godmch.service.getProviderCountryCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.DGUserCallService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.sqlwapper.godmch.providerSupportCountryWapper;
/**
*获取服务商支持国家列表
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_getProviderCountry{
	private static getProviderCountryBean bean;
	private static getProviderCountryCall call;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取服务商支持国家列表");
	}

	@Before
	public void caseUp() {
		bean=new getProviderCountryBean();
		call=new getProviderCountryCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	private void check_verify() throws JSONException, InterruptedException
	{
		Logger.verifyEquals("200", call.getStatusCode1(), "验证status");
		Logger.verifyEquals(true, call.getMessage().contains("请求成功"), "验证msg");
		
		JSONArray resp=call.getCountries();
		providerSupportCountryWapper countryWapper=new providerSupportCountryWapper();
		List<Map> clstList=countryWapper.selectNameByProviderId(bean.getProviderId());
		Logger.verifyEquals(resp.length(), clstList.size(), "验证返回的数据与数据库providerSupportCountry中的一致");
		
		for (int i = 0; i < clstList.size(); i++) {
			Logger.verifyEquals(new org.json.JSONObject(resp.get(i).toString()).get("country"), clstList.get(i).get("supportCountry"), "验证第"+(i+1)+"条country");
			Logger.verifyEquals(new org.json.JSONObject(resp.get(i).toString()).get("countryCode"), clstList.get(i).get("countryCode"), "验证第"+(i+1)+"条countryCode");
			Logger.verifyEquals(new org.json.JSONObject(resp.get(i).toString()).get("enTitle"), clstList.get(i).get("enTitle"), "验证第"+(i+1)+"条enTitle");
			Logger.verifyEquals(new org.json.JSONObject(resp.get(i).toString()).get("cnTitle"), clstList.get(i).get("cnTitle"), "验证第"+(i+1)+"条cnTitle");
			Logger.verifyEquals(new org.json.JSONObject(resp.get(i).toString()).get("areaCode"), clstList.get(i).get("areaCode"), "验证第"+(i+1)+"条areaCode");
			Logger.verifyEquals(new org.json.JSONObject(resp.get(i).toString()).get("currency"), clstList.get(i).get("currency"), "验证第"+(i+1)+"条currency");
			Logger.verifyEquals(new org.json.JSONObject(resp.get(i).toString()).get("continent"), clstList.get(i).get("continent"), "验证第"+(i+1)+"条continent");
			Logger.verifyEquals(new org.json.JSONObject(resp.get(i).toString()).get("flag"), clstList.get(i).get("flag"), "验证第"+(i+1)+"条flag");
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getProviderCountry_001() {
		Logger.start(true,"正例-获取服务商支持国家列表-服务商有支持1个国家");
		try {
			//服务商
			addProviderBean addbean=new addProviderBean();
			addProviderCall addcall=new   addProviderCall();
			
			UserCreateBean usercreateBean=DGUserCallService.initUsercreateBean();
			int userId_channl=DGUserCallService.UserCreateCall(usercreateBean);
			//渠道
			createmchBean createBean=GodmchManageCallService.InitcreatemchBeanForChannel(userId_channl);
			GodmchManageCallService.createmchCallForChannel(createBean);
			
			List<String> countryList=new ArrayList<>();
			countryList.add("CAN");
			List<Integer> serviceType=new ArrayList<>();
			serviceType.add(1);
			
			addbean.setProviderId(userId_channl);
			addbean.setServiceType(serviceType);
			addbean.setCountryList(countryList);
			addcall.setData(addbean);
			addcall.callService();	

			bean.setProviderId(userId_channl);
			call.setData(bean);
			call.callService();			
			
			check_verify();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getProviderCountry_002() {
		Logger.start(true,"反例-获取服务商支持国家列表-服务商支持2个国家");
		try {
			//服务商
			createmchBean pBean=GodmchManageCallService.InitAddProvider();
			int providerId=pBean.getMchId();
			bean.setProviderId(providerId);
			call.setData(bean);
			call.callService();			
			check_verify();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	@Ignore
	public void Tc_getProviderCountry_003() {
		Logger.start(true,"正例-获取服务商支持国家列表-服务商没支持国家-国家 不可能不存在，因为创建的时候是同步，服务商和国家一致");
		try {
			
			call.setData(bean);
			call.callService();			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getProviderCountry_004() {
		Logger.start(true,"正例-获取服务商支持国家列表-服务商为空");
		try {
			
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("200", call.getStatusCode1(), "验证status");
			Logger.verifyEquals(true, call.getMessage().contains("请求成功"), "验证msg");
			Logger.verifyEquals(0, call.getCountries().length(), "验证countries为空");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getProviderCountry_005() {
		Logger.start(true,"正例-获取服务商支持国家列表-服务商不存在");
		try {
			
			bean.setProviderId(9999999);
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("200", call.getStatusCode1(), "验证status");
			Logger.verifyEquals(true, call.getMessage().contains("请求成功"), "验证msg");
			Logger.verifyEquals(0, call.getCountries().length(), "验证countries为空");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}